package com.kizuki.controllor;

import com.kizuki.design.chain.*;
import com.kizuki.design.chain2.Chain2Service;
import com.kizuki.design.chain2.PeopleContent;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ChainController {

    @Resource
    private Chain2Service chain2Service;

    /**
     * 检验客户名字为中文, 密码为复杂密码, 电话格式正确
     * 1.手动指定下一个检验器来形成责任链
     * 2.依次按照顺序执行, 每个检验器都被使用(本例子检验失败直接中断责任链)
     * @param userAccount
     * @return VerifyResult
     */
    @PostMapping("/chain")
    public VerifyResult verify(@RequestBody UserAccount userAccount){
        NameVerify nameVerify = new NameVerify();
        PasswordVerify passwordVerify = new PasswordVerify();
        PhoneVerify phoneVerify = new PhoneVerify();
        //创建责任链
        nameVerify.setNextVerify(passwordVerify);
        passwordVerify.setNextVerify(phoneVerify);
        //开始执行责任链
        return nameVerify.verify(userAccount);
    }

    /**
     * 检验人的性别demo
     * 1.spring形式的注入来形成责任链
     * 2.依次按照顺序执行, 只有支持的handler才会处理
     * @param peopleContent
     */
    @PostMapping("/chain2")
    public void verify2(@RequestBody PeopleContent peopleContent){
        chain2Service.handle(peopleContent);
    }

}
